﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using YWTSSys.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using YWTSSys.Model;
namespace YWTSSys.DAL
{
    public class OMenuDAL
    {
        #region 菜单表处理相关 
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="obj">OMenu 对象</param>
        /// <param name="PermissionTags">对应的功能权限，以,隔开</param>
        /// <returns></returns>
        public static bool AddOMenu(OMenuInfo obj, string PermissionTags)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AddOMenu", obj.MenuName, obj.DWZHref, obj.DWZTarget, obj.DWZRel, obj.DWZFlesh, obj.ParMenuId, obj.DispayIndex);
            int menuid= Convert.ToInt32(DB.ExecuteScalar(dbCommand));

            return string.IsNullOrEmpty(PermissionTags)?menuid>0: AddOMenuPermissionByMenu(menuid, PermissionTags);

        }
       /// <summary>
       /// 删除菜单项,将会删除所有已关联权限信息，包括已分配到用户的权限
       /// </summary>
       /// <param name="MenuId"></param>
        public static bool DeleteOMenuByMenuId(int MenuId)
        {
            try
            {
                Database DB = DatabaseFactory.CreateDatabase();

                DbCommand dbCommand = DB.GetStoredProcCommand("DeleteOMenuByMenuId", MenuId);
                int result = Convert.ToInt32(DB.ExecuteNonQuery(dbCommand));
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 修改菜单信息
        /// </summary>
        public static bool UpdateOMenuByMeunId(OMenuInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UpdateOMenuByMeunId", obj.MenuId, obj.MenuName, obj.DWZHref, obj.DWZTarget, obj.DWZRel, obj.DWZFlesh, obj.ParMenuId, obj.DispayIndex,obj.IsUsed);
            try
            {
                 DB.ExecuteNonQuery(dbCommand);
                 return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        /// <summary>
        /// 菜单分页
        /// </summary>
        /// <param name="MenuName"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Sort"></param>
        /// <param name="TotalCount"></param>
        public static DataTable GetOMenuPagination(string MenuName, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("GetOMenuPagination");
            DB.AddInParameter(dbCommand, "MenuName", DbType.String,MenuName);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        /// <summary>
        /// 获取菜单信息通知ID
        /// </summary>
        /// <param name="MenuId"></param>
        /// <returns></returns>
        public static IDataReader GetOMenuByMenuId(int MenuId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("GetOMenuByMenuId", MenuId);
            return DB.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// 根据ParentId获取其下所有的菜单信息
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public static IDataReader GetAllParentMenu(int parentId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("GetAllParentMenu", parentId);
            return DB.ExecuteReader(dbCommand);
        }

        public static DataTable GetAllOMenu()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("GetAllOMenu");
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        public static IDataReader GetAllOMenu2()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("GetAllOMenu");
            return DB.ExecuteReader(dbCommand);
        }


        #endregion

        #region 菜单功能权限处理相关
        /// <summary>
        /// 单个页面权限添加
        /// </summary>
        /// <param name="MenuId"></param>
        /// <param name="PermissionTag"></param>
        public static bool AddOMenuPermission(int MenuId, string PermissionTag)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AddOMenuPermission", MenuId, PermissionTag);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand))>0;
        }
        /// <summary>
        /// 在添加菜单时批量添加权限信息
        /// </summary>
        /// <param name="MenuId"></param>
        /// <param name="PermissionTags"></param>
        public static bool AddOMenuPermissionByMenu(int MenuId, string PermissionTags)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("AddOMenuPermissionByMenu", MenuId, PermissionTags);

            return DB.ExecuteNonQuery(dbCommand)>0;
        }

        /// <summary>
        /// 删除菜单功能权限,将会删除已分配出去的权限
        /// </summary>
        /// <param name="FunId"></param>
        public static bool DeleteOMenuPermissionByTag(int MenuId,string Tag)
        {
            try
            {
                Database DB = DatabaseFactory.CreateDatabase();

                DbCommand dbCommand = DB.GetStoredProcCommand("DeleteOMenuPermissionByTag", MenuId,Tag);
                int result = Convert.ToInt32(DB.ExecuteNonQuery(dbCommand));
                return true;
            }
            catch
            {
                return false;
            }
        }

        

        /// <summary>
        /// 获取菜单对应的权限
        /// </summary>
        /// <param name="MenuId"></param>
        /// <returns></returns>
        public static IDataReader GetOMenuPermissionByMenuId(int MenuId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("GetOMenuPermissionByMenuId", MenuId);
            return DB.ExecuteReader(dbCommand);
        }

        public static DataTable GetAllOMenuPermission()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("GetAllOMenuPermission");
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        #endregion
    }
}
